package uk.co.q3c.deplan.client.domain.resource;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Id;

import uk.co.q3c.deplan.client.util.Q3DateUtil;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Unindexed
@Cached(expirationSeconds = 600)
public class CalendarAdjustment implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	private Long id;

	private String datekey;
	// WorkingCalendar workingCalendar;

	private Date date;

	private int adjustment;

	private boolean absolute;

	public CalendarAdjustment() {
		super();
	}

	public Date getDate() {
		if (date == null) {
			return null;
		}
		return new Date(date.getTime());
	}

	public void setDate(Date date) {
		this.date = new Date(date.getTime());
	}

	public int getAdjustment() {
		return adjustment;
	}

	public void setAdjustment(int adjustment) {
		this.adjustment = adjustment;
	}

	public boolean isAbsolute() {
		return absolute;
	}

	/**
	 * Example: If the adjustment is absolute at 420, combining this adjustment with default working time will result in 7hrs working time
	 * regardless of the default setting
	 * 
	 * Example: If the adjustment is not absolute (i.e relative) at 420, then the 420 is added to the default working time - which if it
	 * already at 420 would result in a working time of 840 minutes
	 * 
	 * @param absolute
	 */
	public void setAbsolute(boolean absolute) {
		this.absolute = absolute;
	}

	public String getDatekey() {

		return datekey;
	}

	/**
	 * @param datekey
	 */
	public void setDatekey(String key) {
		this.datekey = key;

	}

	@Override
	public String toString() {
		return "CalendarAdjustment [absolute=" + absolute + ", adjustment=" + adjustment + ", dateIndex=" + toKey(date) + "]";
	}

	private String toKey(Date date) {
		return Q3DateUtil.dateToKey(date);
	}

	public Long getId() {
		return id;
	}

	// public WorkingCalendar getWorkingCalendar() {
	// return workingCalendar;
	// }
	//
	// public void setWorkingCalendar(WorkingCalendar workingCalendar) {
	// this.workingCalendar = workingCalendar;
	// }

	// @Override
	// public void readXML(XMLImporter importer, Element element) {
	// adjustmentId = XML.readElementInteger(element, ADJUSTMENT_ID);
	// dateIndex = XML.readElementDate(element, DATEINDEX);
	// adjustment = XML.readElementInteger(element, ADJUSTMENT);
	// absolute = XML.readElementBoolean(element, ABSOLUTE);
	// importer.save(this);
	// }
	//
	// @Override
	// public void writeXML(Element parentElement) {
	// XML.createElement(parentElement, ADJUSTMENT_ID, adjustmentId);
	// XML.createElement(parentElement, DATEINDEX, dateIndex);
	// XML.createElement(parentElement, ADJUSTMENT, adjustment);
	// XML.createElement(parentElement, ABSOLUTE, absolute);
	//
	// }

}
